package interfaceImp;
import java.rmi.RemoteException;

import cipher.SimpleStringCipher;
import interfaces.Shoot;


public class ShootImpl implements Shoot {

	public static int ROW_SIZE = 10;
    public static int SIZE = 10;
    public static int PLAYER_AMOUNT = 4;
    public int[][] mainTab = new int[ROW_SIZE*SIZE*PLAYER_AMOUNT/2][ROW_SIZE*SIZE*PLAYER_AMOUNT/2];
	
    
    // 0  pudlo nie trafion
    // 1 trfione
	@Override
	//public synchronized String getandsetShoot(int id, int x, int y) throws RemoteException {
		public synchronized String getandsetShoot(String encrypted) throws Exception,RemoteException {
		StringBuffer shoot = new StringBuffer();
		int id,x,y;
		String decrypted = SimpleStringCipher.decrypt(encrypted);
		id= Integer.parseInt(decrypted.substring(0,2));
		x = Integer.parseInt(decrypted.substring(3,5));
		y = Integer.parseInt(decrypted.substring(6,8));
		switch(mainTab[x][y]){
		case 0: shoot.append(id + ";" + x +";" + y + ";" + 1); mainTab[x][y]=1; break;	//pudlo
		case 1: shoot.append(id + ";" + x +";" + y + ";" + 1); mainTab[x][y]=1; break;	//pudlo juz trafione
		case 2: shoot.append(id + ";" + x +";" + y + ";" + 2); mainTab[x][y]=2; break;  //trafiony juz trafione
		case 3: shoot.append(id + ";" + x +";" + y + ";" + 2); mainTab[x][y]=2; break;	//ustawiony -> trafiony
		}
		System.out.println("shoot on: " + x + "," + y + " : " + mainTab[x][y] + " by " + id);
		return shoot.toString();
		
	}


	@Override
	public void setMainTab(int[][] tab) throws RemoteException {
		mainTab=tab;
		
	}
	public void setShot(int x, int y) throws RemoteException{
//		x=x/ROW_SIZE;
//		y=y/ROW_SIZE;
		mainTab[x][y]=2; //statek = czerwony
		System.out.println(x + "," + y + " : " + mainTab[x][y]);
		System.out.println("shoot on: " + x + "," + y + " : " + mainTab[x][y]);
 	}


	@Override
	public void placeShip(String encrypted) throws Exception,RemoteException {
		int x,y;
		String decrypted = SimpleStringCipher.decrypt(encrypted);
		x = Integer.parseInt(decrypted.substring(0,2));
		y = Integer.parseInt(decrypted.substring(3,5));
		mainTab[x][y]=3; //statek = czerwony
		
		
	}


	@Override
	public int[][] getMainTab() throws RemoteException {
		return mainTab;
	}
	
	
	

}
